package com.jyu.apts.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.entity.Item;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.service.ItemService;
import com.jyu.apts.service.OrganizationService;
import com.jyu.apts.utils.AliyunOSSUtil;

@RestController
public class ItemController {

	@Autowired
	ItemService itemService;
	@Autowired
	OrganizationService organizationService;
	
	@RequiresPermissions("item_html")
	@RequestMapping("/item_html")
	public ModelAndView item_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/item.html");
		return mv;
	}
	
	@RequiresPermissions("item_html")
	@RequestMapping("/addItem")
	public ResultDto addItem(String name,long organizationId,MultipartFile image,String price) {
		Organization organization = organizationService.getOne(organizationId);
		if(organization == null) {
			return new ResultDto("机构不存在",null,2);
		}
		Item item = new Item();
		item.setPrice(price);
		if(image!=null) {
			String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf('.') + 1);
			try {
				String objectName = AliyunOSSUtil.upload(image.getInputStream(), suffix);
				item.setImage("/oss/"+objectName);
			} catch (IOException e) {
				e.printStackTrace();
				return new ResultDto("上传失败", null, 2);
			}
		}
		item.setName(name);
		item.setOrganization(organization);
		itemService.save(item);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("item_html")
	@RequestMapping("/upItem")
	public ResultDto upItem(long id,String name,long organizationId,MultipartFile image,String price) {
		Item item = itemService.getOne(id);
		if(item == null) {
			return new ResultDto("商品不存在!",null,2);
		}
		item.setPrice(price);
		Organization organization = organizationService.getOne(organizationId);
		if(organization == null) {
			return new ResultDto("机构不存在",null,2);
		}
		if(image!=null) {
			String oldImg = item.getImage();
			if(oldImg!=null && oldImg.startsWith("/oss/")) {
				String oldObjectName =  oldImg.substring(5);
				AliyunOSSUtil.delete(oldObjectName);
			}
			String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf('.') + 1);
			try {
				String objectName = AliyunOSSUtil.upload(image.getInputStream(), suffix);
				item.setImage("/oss/"+objectName);
			} catch (IOException e) {
				e.printStackTrace();
				return new ResultDto("上传失败", null, 2);
			}
		}
		item.setOrganization(organization);
		item.setName(name);
		itemService.save(item);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("item_html")
	@RequestMapping("/delItem")
	public ResultDto delItem(String ids) {
		String[] idArray = ids.split(",");
		List<Item> items = new ArrayList<Item>();
		for(String id : idArray) {
			Item item = new Item();
			item.setId(new Long(id));
			items.add(item);
		}
		itemService.delete(items);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequestMapping("/getItemPage")
	public TableDto<ItemDto> getItemPage(Integer pageNumber,Integer pageSize,Long organizationId,String search) {
		Page<Item> pageResult = itemService.getItemPage(pageNumber, pageSize, organizationId, search);
		List<Item> items = pageResult.getContent();
		List<ItemDto> itemDtos = new ArrayList<ItemDto>();
		items.forEach(item->itemDtos.add(new ItemDto().convert(item)));
		return new TableDto<ItemDto>(itemDtos,pageResult.getTotalElements());
	}
	
	@RequiresPermissions("item_html")
	@RequestMapping("/getAllItemByOrganization")
	public List<ItemDto> getAllItemByOrganization(long organizationId) {
		Organization organization = organizationService.getOne(organizationId);
		List<ItemDto> itemDtos = new ArrayList<ItemDto>();
		List<Item> items = organization.getItems();
		items.forEach(item->itemDtos.add(new ItemDto().convert(item)));
		return itemDtos;
	}
	
}
